<?php
/**
 * Created by.
 * User: littleU-569598569
 * Date: 2021/2/3 0003
 * Time: 20:33
 */

namespace app\api\service;

use app\api\model\AppletHomeBackground as AppletHomeBackgroundModel;
use app\api\model\AppletBanner as AppletBannerModel;
use app\api\model\AppletHeader as AppletHeaderModel;


class Applet
{
    public static function getHomeBackground()
    {
        $result = AppletHomeBackgroundModel::get(1);
        return json([
            'code' => config('code.success'),
            'data' => $result
        ]);
    }

    public function setHomeBackground($data)
    {
        $id = $data['id'];
        try
        {
            $obj = new AppletHomeBackgroundModel();
            $obj->allowField(true)->save($data, ['id' => $id]);
        }
        catch (\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg'  => $ex->getMessage()
            ]);
        }
        return json([
            'code' => config('code.success'),
            'msg'  => '编辑成功'
        ]);
    }

    public static function getBannerList()
    {
        $banner = AppletBannerModel::all(['status' => config('code.normal')]);
        return json([
            'code' => config('code.success'),
            'data' => $banner
        ]);
    }

    public function setBanner($data)
    {
        $id = $data['id'];
        if($id == '')
        {
            return $this->addBanner($data);
        }
        else
        {
            return $this->updateBanner($data, $id);
        }
    }

    private function addBanner($data)
    {
        try
        {
            $obj = new AppletBannerModel($data);
            $obj->allowField($data)->save();
        }
        catch (\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg'  => $ex->getMessage()
            ]);
        }
        return json([
            'code' => config('code.success'),
            'msg'  => '新增成功'
        ]);
    }

    private function updateBanner($data, $id)
    {
        try
        {
            $obj = new AppletBannerModel();
            $obj->allowField(true)->save($data, ['id'=>$id]);
        }
        catch (\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg'  => $ex->getMessage()
            ]);
        }
        return json([
            'code' => config('code.success'),
            'msg'  => '编辑成功'
        ]);
    }

    public static function getAppletHeader()
    {
        $result = AppletHeaderModel::get(1);
        return json([
            'code' => config('code.success'),
            'data' => $result
        ]);
    }

    public function setAppletHeader($data)
    {
        $id = $data['id'];
        try
        {
            $obj = new AppletHeaderModel();
            $obj->allowField(true)->save($data, ['id' => $id]);
        }
        catch (\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg'  => $ex->getMessage()
            ]);
        }
        return json([
            'code' => config('code.success'),
            'msg'  => '编辑成功'
        ]);
    }
}